package net.maku.im.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.im.dto.ImGroupBanDTO;
import net.maku.im.dto.ImGroupMemberQueryDTO;
import net.maku.im.dto.ImGroupQueryDTO;
import net.maku.im.dto.ImGroupUnbanDTO;
import net.maku.im.service.ImGroupMemberService;
import net.maku.im.service.ImGroupService;
import net.maku.im.vo.ImGroupMemberVO;
import net.maku.im.vo.ImGroupVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * IM群
 *
 * @author Blue
 * @since 1.0.0 2024-07-14
 */
@RestController
@RequestMapping("im/group")
@Tag(name = "IM群组")
@AllArgsConstructor
public class ImGroupController {

    private final ImGroupService groupService;

    private final ImGroupMemberService groupMemberService;

    @GetMapping("findByName")
    @Operation(summary = "根据名称查找群组",description = "用于下拉框选择群组")
    public Result<List<ImGroupVO>> findByName(@RequestParam String name){
        List<ImGroupVO> vos = groupService.findByName(name);
        return Result.ok(vos);
    }

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('im:group:page')")
    public Result<PageResult<ImGroupVO>> page(@ParameterObject @Valid ImGroupQueryDTO query) {
        PageResult<ImGroupVO> page = groupService.page(query);
        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('im:group:info')")
    public Result<ImGroupVO> get(@PathVariable("id") Long id) {
        ImGroupVO vo = groupService.info(id);
        return Result.ok(vo);
    }

    @Operation(summary = "封禁")
    @PutMapping("ban")
    @PreAuthorize("hasAuthority('im:group:ban')")
    public Result<String> ban(@RequestBody @Valid ImGroupBanDTO dto) {
        groupService.ban(dto);
        return Result.ok();
    }

    @Operation(summary = "解封")
    @PutMapping("unban")
    @PreAuthorize("hasAuthority('im:group:ban')")
    public Result<String> unban(@RequestBody @Valid ImGroupUnbanDTO dto) {
        groupService.unban(dto);
        return Result.ok();
    }

    @GetMapping("member/page")
    @Operation(summary = "分页查询群成员")
    @PreAuthorize("hasAuthority('im:group:member')")
    public Result<PageResult<ImGroupMemberVO>> page(@ParameterObject @Valid ImGroupMemberQueryDTO query) {
        PageResult<ImGroupMemberVO> page = groupMemberService.page(query);
        return Result.ok(page);
    }

}